package net.wxxr.http.engine;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

import net.wxxr.http.config.HttpContans;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.request.HttpPostMutipleRequest;
import net.wxxr.http.request.HttpPutUploadByteRequest;
import net.wxxr.utils.StringUtil;
import oauth.signpost.http.HttpParameters;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.ByteArrayBuffer;

public class HttpPutUploadByteEngine extends HttpInIdempotentEngine {

	public HttpPutUploadByteEngine(HttpBaseRequest request) {
		super(request);
	}

	@Override
	protected void initTag() {
		TAG = "HttpPostUploadByteEngine";
	}
	protected void initRequest() {
		requestBase = new HttpPut(baseRequest.getUrl());
	}

	/**
	 * 实体数据的设置
	 */
	@Override
	protected void setRequestParams() throws UnsupportedEncodingException {

		HttpPutUploadByteRequest postRequest = (HttpPutUploadByteRequest) baseRequest;
		AbstractHttpEntity entity = null;
		File file = null;
		String data = null;
		String contentType = postRequest.getContentType();
		if (HttpContans.CONTENT_TYPE_JSON.equals(contentType)) {
			try {
				// 需要上传的文件
				file = postRequest.getFile();
				// 额外的参数(json格式的数据)
				data = postRequest.getBodyData();

				ByteArrayBuffer bytBuffer = new ByteArrayBuffer(0);
				byte[] buf = getBuf(file, bytBuffer);
				
				if (null != data) {
					/**
					 *  额外数据 + 文件 
					 *   (
					 *   这里兼容以前的方案而设计，此方案效率很低，不推荐使用，
					 *   推荐使用HttpPostMutipleEngine这个类取实现此方案
					 *   )
					 */
					String file2string = StringUtil.byte2hex(buf);
					data = data.replace("#", file2string);
					entity = new StringEntity(data, HttpContans.CHARSET_GBK);
				} else {// 只对文件发送
					//entity = new StringEntity(file2string, HttpContans.CHARSET_GBK);
					entity = new ByteArrayEntity(buf);
				
				}

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else if (HttpContans.CONTENT_TYPE_XML.equals(contentType)) {
		}
		// 服务器告诉浏览器自己响应的对象的类型
		if (null != contentType) {
			if (HttpContans.CONTENT_TYPE_JSON.equals(contentType)) {
				// 发送的参数文本格式为json格式
				entity.setContentType("application/json");
			} else if (HttpContans.CONTENT_TYPE_XML.equals(contentType)) {
				// 发送的参数文本格式为xml格式
				entity.setContentType("application/xml");
			} else if(HttpContans.CONTENT_TYPE_MAP.equals(contentType)){
				// 发送的参数文本格式为?name=value&name2=value2&name3=value3格式
				entity.setContentType("application/x-www-form-urlencoded");
			}
		}
		((HttpPost) requestBase).setEntity(entity);
	}

	private byte[] getBuf(File file, ByteArrayBuffer bytBuffer)
			throws FileNotFoundException, IOException {
		
		FileInputStream in = new FileInputStream(file);
		byte[] buf = new byte[in.available()];
		int num = 0;
		while ((num = in.read(buf, 0, buf.length)) != -1) {
			bytBuffer.append(buf, 0, num);
		}
		
		buf = bytBuffer.buffer();
		
		bytBuffer.clear();
		return buf;
	}

}
